//
//  ZYHomePopView.m
//  ZYOils
//
//  Created by Mr.Xr on 2019/10/18.
//  Copyright © 2019 Mr.Xr. All rights reserved.
//

#import "ZYHomePopView.h"
#import "ZYOilsInfoModel.h"

@interface ZYHomePopView()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewHeightLayout;

@property (weak, nonatomic) IBOutlet UIView *oilContentView;

@property (weak, nonatomic) IBOutlet UITextField *moneyTextField;

@property (weak, nonatomic) IBOutlet UILabel *label;



@end

@implementation ZYHomePopView

- (void)setList:(NSArray *)list {
    _list = list;
    
    NSInteger spacing = 20;
    CGFloat   btnWidth = (SCREEN_WIDTH - 120 - spacing * 2)/3;
    CGFloat   btnHeight = 40;
    
    [list enumerateObjectsUsingBlock:^(ZYOilsInfoModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [self getCustomButton:model];
        btn.tag = idx + 1;
        [self.oilContentView addSubview:btn];
            btn.frame = CGRectMake(idx%3 * (btnWidth + spacing), (btnHeight+10) * (idx/3), btnWidth, btnHeight);
    }];
    NSLog(@"%@", ZYUserInfoModel.sharedInfoModel.userModel.companyName);
    self.nameLabel.text = ZYUserInfoModel.sharedInfoModel.userModel.companyName;
    
    self.contentViewHeightLayout.constant = 40 + list.count/3 * (40 + 10);
    [self layoutIfNeeded];
}

- (UIButton *)getCustomButton:(ZYOilsInfoModel *)model {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:model.oilName forState:UIControlStateNormal];
    [btn setTitleColor:COLORHEX(@"#777777") forState:UIControlStateNormal];
    [btn setTitleColor:COLORHEX(@"#CCAD7B") forState:UIControlStateSelected];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    btn.layer.borderColor = COLORHEX(@"#777777").CGColor;
    btn.layer.borderWidth = 0.5;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (void)btnClick:(UIButton *)sender {
    for (UIButton *btn in self.oilContentView.subviews) {
        btn.selected = NO;
        btn.layer.borderColor = COLORHEX(@"#777777").CGColor;
    }
    
    sender.selected = YES;
    sender.layer.borderColor = COLORHEX(@"#CCAD7B").CGColor;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:nil];
    [LBLimitInput limitInputView:self.moneyTextField regX:@"^\\-?([0-9]\\d{0,5})(\\.\\d{0,2})?$"];

}

- (void)textChange:(NSNotification *)sender {
    UITextField *object = sender.object;
    self.label.hidden = object.text.length == 0 ? NO : YES;
}


-(void)dealloc{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (IBAction)cancelBtnClick:(id)sender {
    [self removeFromSuperview];
}


- (IBAction)yesBtnClick:(id)sender {
    
    if ([LBLimitInput isEmpty:self.moneyTextField.text]) {
        [ZYHud showErrorText:@"请输入价格"];
        return;
    }
    
    for (UIButton *btn in self.oilContentView.subviews) {
        if (btn.selected) {
            ZYOilsInfoModel *model = self.list[btn.tag - 1];
            NSLog(@"%@", model.oilName);
            [self.subject sendNext:@{@"model": model,
                                     @"price": self.moneyTextField.text}];
            [self removeFromSuperview];
            return;
        }
    }
    
    [ZYHud showErrorText:@"请选择油品"];
    
}

- (RACSubject *)subject {
    if (!_subject) {
        _subject = [RACSubject subject];
    }
    return _subject;
}


@end
